package sg.com.techstudio.ecc.dao;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import sg.com.techstudio.ecc.obj.LearningJourneyObj;
import sg.com.techstudio.ecc.obj.SessionObj;
import sg.com.techstudio.ecc.obj.SpeakerObj;
import sg.com.techstudio.ecc.obj.SystemConfigObj;

@Repository("systemConfigDao")
@Transactional
public class SystemConfigDao {
	private static final Log log = LogFactory.getLog(SystemConfigDao.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<SessionObj> sessionList(){
		log.debug("sessionList() entry");
		Criteria c = sessionFactory.getCurrentSession().createCriteria(SessionObj.class);
		c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		c.addOrder(Order.asc("type"));
		c.addOrder(Order.asc("no"));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<SessionObj> sessionList(Integer type){
		log.debug("sessionList() entry");
		Criteria c = sessionFactory.getCurrentSession().createCriteria(SessionObj.class);
		if(type!=null && type>0 && type<5)
			c.add(Restrictions.or(Restrictions.eq("type", 5),Restrictions.eqOrIsNull("type", type)));
		c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		c.addOrder(Order.asc("type"));
		c.addOrder(Order.asc("no"));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<LearningJourneyObj> learningJourneyList(){
		log.debug("sessionList() entry");
		Criteria c = sessionFactory.getCurrentSession().createCriteria(LearningJourneyObj.class);
		c.addOrder(Order.asc("id"));
		return c.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<SpeakerObj> speakerList(){
		log.debug("speakerList() entry");
		Criteria c = sessionFactory.getCurrentSession().createCriteria(SpeakerObj.class);
		c.addOrder(Order.asc("id"));
		return c.list();
	}
	
	public List<String> countryList() {
		log.debug("countryList() entry");
		List<SystemConfigObj> list = valueList("country");
		List<String> ret = new LinkedList<String>();
		if(list!=null && !list.isEmpty())
			for(SystemConfigObj sc: list)
				ret.add(sc.getConfigValue());
		return ret;
	}
	
	public boolean validatePromoCode(String promoCode) {
		log.debug("validatePromoCode() entry");
		Criteria c = sessionFactory.getCurrentSession().createCriteria(SystemConfigObj.class);
		c.add(Restrictions.eq("vflag", true));
		c.add(Restrictions.ilike("configType", "promo_code"));
		c.add(Restrictions.eq("configValue", promoCode));
		c.setProjection(Projections.rowCount());
		return 0<(Long)c.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<SystemConfigObj> valueList(String configType) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(SystemConfigObj.class);
		c.add(Restrictions.eq("vflag", true));
		c.add(Restrictions.ilike("configType", configType));
		return c.list();
	}
}
